﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Model.DTO;
namespace JLL.China.Infrastructure.Repositories.ProjectDirectory 
{
    public class ProjectDirectoryRepository : SQLRepositoryBase<Model.ProjectDirectory, int>, IProjectDirectoryRepository
    {
        public override Model.ProjectDirectory FindBy(int key)
        {
            var listing = DataContext.ProjectDirectories.SingleOrDefault(x => key.Equals(x.Listing_ID));

            if (listing != null)
            {
                return ProjectDirectoryFactory.FromLINQObject(listing);
            }
            else
            {
                return null;
            }

        }
        public override IList<Model.ProjectDirectory> FindAll()
        {
            throw new NotImplementedException();
        }

        protected override void PersistNewItem(Model.ProjectDirectory item)
        {
            DataContext.ProjectDirectories.InsertOnSubmit(ProjectDirectoryFactory.ToLINQObject(item));

        }
        protected override void PersistUpdatedItem(Model.ProjectDirectory item)
        {

            var listing = DataContext.ProjectDirectories.SingleOrDefault(x => x.Listing_ID.Equals(item.ID));

            if (listing != null)
            {
                listing.Longitude = item.Longitude;
                listing.Latitude = item.Latitude;
                
            }
            else
            {
                throw new Exception("Can't update!");
            }

        }
        protected override void PersistDeletedItem(Model.ProjectDirectory item)
        {
            throw new NotImplementedException();
        }

        public Model.ProjectDirectory FindById(int id)
        {
            var listing = DataContext.ProjectDirectoryItems.SingleOrDefault(x => x.Listing_ID.Equals(id));

            if (listing != null)
            {
                return new Model.ProjectDirectory
                {
                    ID = Int32.Parse(listing.Listing_ID.ToString()),
                    ProjectNameFID = listing.ProjectNameFID,
                    PropertyClsssm = listing.PropertyClsssm,
                    StreetNameFID = listing.StreetNameFID,
                    PropertyTypeFID = listing.PropertyTypeFID,
                    CountryFID = listing.CountryFID,
                    CityFID = listing.CityFID,
                    LocaltionFID = listing.LocaltionFID,
                    SubAreaFID = listing.SubAreaFID,
                    Tenure = listing.Tenure,
                    Longitude = listing.Longitude,
                    Latitude = listing.Latitude,
                    HotImage_FID = listing.HotImage_FID,
                    HotImage_Path = listing.HotImage_Path,
                    CreateDate = listing.CreateDate,
                    ListingDate = listing.ListingDate,
                    ListingExpiry = listing.ListingExpiry,
                    Description = listing.Description,
                    DescriptionLocal = listing.DescriptionLocal,
                    StatusFID = listing.StatusFID,
                    ProjectName = listing.ProjectName,
                    StreetName = listing.StreetName,
                    ProjectNameLocal = listing.ProjectNameLocal,
                    StreetNameLocal = listing.StreetNameLocal,
                    Country=listing.Country,
                    City =listing.City,
                    HouseNumber = listing.HouseNumber,
                    Facilities = listing.Facilities,
                    Features = listing.Features
                };

            }
            else
            {
                return null;
            }



        }

        public IList<Model.ProjectDirectory> Search(ListingSearchCriteria searchCriteria)
        {

            var items = SearchByCriteria(searchCriteria).OrderBy(x => x.Listing_ID);
            var result = new List<Model.ProjectDirectory>();
            var list = items.ToList();

            foreach (var listing in list)
            {
                result.Add(new Model.ProjectDirectory
                {

                    ID = Int32.Parse(listing.Listing_ID.ToString()),
                    ProjectNameFID = listing.ProjectNameFID,
                    PropertyClsssm = listing.PropertyClsssm,
                    StreetNameFID = listing.StreetNameFID,
                    PropertyTypeFID = listing.PropertyTypeFID,
                    CountryFID = listing.CountryFID,
                    CityFID = listing.CityFID,
                    LocaltionFID = listing.LocaltionFID,
                    SubAreaFID = listing.SubAreaFID,
                    Tenure = listing.Tenure,
                    Longitude = listing.Longitude,
                    Latitude = listing.Latitude,
                    HotImage_FID = listing.HotImage_FID,
                    HotImage_Path = listing.HotImage_Path,
                    CreateDate = listing.CreateDate,
                    ListingDate = listing.ListingDate,
                    ListingExpiry = listing.ListingExpiry,
                    Description = listing.Description,
                    DescriptionLocal = listing.DescriptionLocal,
                    Facilities = listing.Facilities,
                    Features = listing.Features,
                    ProjectName = listing.ProjectName,
                    City = listing.City,
                    Location = listing.Location,
                    SubArea = listing.Subarea,
                    PropertyTypeName = listing.PropertyType,
                    StatusFID = listing.StatusFID,
                    ListingSource = listing.ListingSource,
                    StreetName = listing.StreetName,
                    UpdateBy = listing.UpdateBy,
                    CreateBy = listing.CreateBy,
                    UpdateDate = listing.UpdateDate,
                    ProjectNameLocal = listing.ProjectNameLocal,
                    StreetNameLocal = listing.StreetNameLocal,
                    Country = listing.Country,
                    HouseNumber = listing.HouseNumber
                });
            }

            return result;
        }
        private IQueryable<LINQ2SQL.ProjectDirectoryItem> SearchByCriteria(ListingSearchCriteria searchCriteria)
        {
            //
          
            var items = (from m in DataContext.ProjectDirectoryItems
                         select m);

            if (searchCriteria.CountryID.HasValue)
            {
                //Local or International
                items = items.Where(x => x.CountryFID == searchCriteria.CountryID.Value);
            }
            else
            {
                int countryID = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["CountryID"].ToString());
                items = items.Where(x => x.CountryFID != countryID);

            }

          
            if (searchCriteria.Status.HasValue)
            {
                // Status
                items = items.Where(x => x.StatusFID.Equals(searchCriteria.Status));
            }
            //Search by City
            if (searchCriteria.CityID.HasValue)
            {
                //Local or International
                items = items.Where(x => x.CityFID == searchCriteria.CityID.Value);
            }
            //Search by Location
            if (searchCriteria.LocationID.HasValue)
            {
                //Local or International
                items = items.Where(x => x.LocaltionFID == searchCriteria.LocationID.Value);
            }
            //Search by Subarea
            if (searchCriteria.SubAreaID.HasValue)
            {
                //Local or International
                items = items.Where(x => x.SubAreaFID == searchCriteria.SubAreaID.Value);
            }
            //Search by PropertyType
            if (searchCriteria.PropertyTypeID.HasValue)
            {
                //Local or International
                items = items.Where(x => x.PropertyTypeFID == searchCriteria.PropertyTypeID.Value);
            }
            //Search by Property Classification
            if (string.IsNullOrEmpty(searchCriteria.PropertyClassification)==false)
            {
                //Local or International
                items = items.Where(x => x.PropertyClsssm.Equals(searchCriteria.PropertyClassification));
            }
            //
            //Search by Tenure
            if (string.IsNullOrEmpty(searchCriteria.Tenure) == false)
            {
                //Local or International
                items = items.Where(x => x.Tenure.Contains(searchCriteria.Tenure.Trim()));
            }
            //Search by Keyword
            if (string.IsNullOrEmpty(searchCriteria.Keyword) == false)
            {
                //Local or International
                items = items.Where(x => x.ProjectName.Contains(searchCriteria.Keyword.Trim()));
            }
            //
            if (string.IsNullOrEmpty(searchCriteria.CityName) == false)
            {
                //Local or International
                items = items.Where(x => x.City.Contains(searchCriteria.CityName.Trim()));
            }
            return items;
        }


        public int Post(Model.ProjectDirectory l)
        {
            int? ID = null;
            if (l.ID != 0)
            {
                ID = l.ID;
            }
            int listingid = DataContext.sp_projectdir_listing(ID,l.ProjectNameFID,l.StreetNameFID,l.PropertyClsssm,l.PropertyTypeFID,l.CountryFID,l.CityFID,l.LocaltionFID,l.SubAreaFID,l.Tenure,l.Description,l.DescriptionLocal,l.City,l.CreateBy,l.UpdateBy,l.ListingSource,l.HouseNumber);
            return listingid;

        }
        public void ChangeStatus(string arrayId, int status)
        {
            DataContext.sp_ProjectDirectory_change_status(arrayId, status);
         
        }
        //
        public void SaveProjectDetail(Model.ProjectDirectoryDetail item)
        {
            DataContext.sp_projectdir_detail(item.ListingFID, item.LanguageID, item.Developer, item.Company, item.SiteArea, item.Carpark, item.TotalUnit, item.ExpectedTOP, item.YearCompletion, item.Specifications, item.BuildingDesign, item.Concept, item.Architect, item.Landscape);
        }
        //
        public void UpdateFacilitiesFeatures(int id,string features,string facilities)
        {
            DataContext.sp_projectdir_facilities_features(id, features, facilities);
        }
        //
        public Model.ProjectDirectoryDetail GetProjectDirDetail(int id,int lang)
        {
           var item = DataContext.ProjectDirectoryDescItems.SingleOrDefault(x => x.ListingFID.Equals(id) && x.LanguageFID.Equals(lang));
           if (item != null)
           {
               return new Model.ProjectDirectoryDetail
               {
                   ListingFID = item.ListingFID,
                   LanguageID = item.LanguageFID,
                   Developer = item.Developer,
                   Company = item.Company,
                   SiteArea = item.SiteArea,
                   Carpark = item.Carpark,
                   TotalUnit = item.TotalUnit,
                   ExpectedTOP = item.ExpectedTOP,
                   YearCompletion = item.YearCompletion,
                   Specifications = item.Specifications,
                   BuildingDesign = item.BuildingDesign,
                   Concept = item.Concept,
                   Architect = item.Architect,
                   Landscape = item.Landscape
               };
           }
           else
           {
               return null;
           }
        }
        //
        public decimal GetIdByProjectNameId(int id)
        {
            var item = DataContext.ProjectDirectories.SingleOrDefault(x => x.ProjectNameFID.Equals(id));
            if(item!=null){
                return item.Listing_ID;
            }
            return 0;
        }
    }
}
